//
//  WBHomeViewController.swift
//  weibo
//
//  Created by Felix on 16/11/4.
//  Copyright © 2016年 Felix. All rights reserved.
//

import UIKit

class WBHomeViewController: WBBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
      
        //注册POPView弹出时候的通知
        NotificationCenter.default.addObserver(self, selector: #selector(titleChange), name: NSNotification.Name(rawValue:WBPresentationManagerDidPresented), object: nil)
        
        //注册popView消失时候的通知
         NotificationCenter.default.addObserver(self, selector: #selector(titleChange), name: NSNotification.Name(rawValue:WBPresentationManagerDidDismissed), object: nil)
        
        //1.设置访客视图
        if !isLogin {
            visitorView?.setUpVisitorViewInfo(imageName: nil, title: "我是首页")
        }else{
             navigationItem.titleView = titleButton
        }
        
        
        //2.设置导航栏左右两边图片
        navigationItem.leftBarButtonItem = UIBarButtonItem.init("navigationbar_friendattention", target: self, action: #selector(leftBtnClick))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem.init("navigationbar_pop", target: self, action: #selector(rightBtnClick))
        
       

    }
    
    @objc private func titleBtnClick(btn:UIButton){
        
        
        //1.获取需要modal的控制器
        let sb = UIStoryboard.init(name: "WBPopOver", bundle: nil)
        guard let menuView = sb.instantiateInitialViewController() else {
            return
        }
        
        //2.设置代理
        
        //这里用menuView.transitioningDelegate = WBPresentationManager()这种方法会无效
        
        menuView.transitioningDelegate = animatorManager
        
        //3.设置modal方式为自定义
        menuView.modalPresentationStyle = UIModalPresentationStyle.custom
        
        //4.弹出视图控制器
        present(menuView, animated: true, completion: nil)
    }
    
    @objc private func rightBtnClick(){
        let sb = UIStoryboard.init(name:"WBQRCode",bundle:nil)
        guard let QRCode = sb.instantiateInitialViewController() else {
            return
        }
        present(QRCode ,animated:true ,completion:nil)
    }
    @objc private func leftBtnClick(){
        AdaLog(message: "左边按钮点击")
    }
    
    
    
    @objc private func titleChange(){
        titleButton.isSelected = !titleButton.isSelected
    }
    
    
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - 懒加载
    private lazy var animatorManager: WBPresentationManager = {
        let manager = WBPresentationManager()
        manager.presentFrame = CGRect(x: 100, y: 60, width: 200, height: 300)
        return manager
    }()
    
    
    private lazy var titleButton :WBTitleButton = {
        //3.添加顶部视图
        let titleView = WBTitleButton.init()
        titleView .addTarget(self, action: #selector(titleBtnClick(btn:)), for: UIControlEvents.touchUpInside)
        titleView.setTitle(WBUserAccount.userAccount?.screen_name , for: UIControlState.normal)
        return titleView
    }()
    
}

